home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte17 / fndfcnot.c < prev    next >
C/C++ Source or Header  |  1996-01-31  |  9KB  |  281 lines

  1.  
  2. #include <windows.h>  
  3. #include "FndFCNot.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  22.  
  23. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  24.                       LPTSTR lpCmdLine, int nCmdShow)
  25. {
  26.    MSG      msg;
  27.    HWND     hWnd; 
  28.    WNDCLASS wc;
  29.  
  30.    // Register the main application window class.
  31.    //............................................
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    // Create the main application window.
  54.    //....................................
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. static HANDLE hWait = NULL;
  108. static int    nTab  = 50;
  109.  
  110. DWORD DirChanges( LPDWORD lpdwParam )
  111. {
  112.    HWND   hWnd = (HWND)lpdwParam;
  113.  
  114.    // Process forever, or until terminated.
  115.    //......................................
  116.    while( TRUE )
  117.    {
  118.       // Wait for a change notification to happen.
  119.       //..........................................
  120.       if ( WaitForSingleObject( hWait, INFINITE ) == WAIT_OBJECT_0 )
  121.       { 
  122.          // Update the window with new information.
  123.          //........................................
  124.          InvalidateRect( hWnd, NULL, TRUE );
  125.          UpdateWindow( hWnd );
  126.       }
  127.  
  128.       // Find next change notification.
  129.       //...............................
  130.       FindNextChangeNotification( hWait );
  131.    }
  132.  
  133.    return( 0 );
  134. }
  135.  
  136. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  137. {
  138. static DWORD  dwID;
  139. static char   szOut[128];
  140. static HANDLE hThread = NULL;
  141.  
  142.    switch( uMsg )
  143.    {
  144.       case WM_CREATE :
  145.               {
  146.                  // Start change notification.
  147.                  //...........................
  148.                  hWait = FindFirstChangeNotification( "D:\\TEMP", FALSE, 
  149.                                            FILE_NOTIFY_CHANGE_FILE_NAME |
  150.                                            FILE_NOTIFY_CHANGE_SIZE |
  151.                                            FILE_NOTIFY_CHANGE_LAST_WRITE );
  152.  
  153.                  // Create a thread to monitor changes.
  154.                  //....................................
  155.                  hThread = CreateThread( NULL, 0, 
  156.                                       (LPTHREAD_START_ROUTINE)DirChanges,
  157.                                       hWnd, 0, &dwID );
  158.               }
  159.               break;
  160.  
  161.       case WM_PAINT :
  162.               { 
  163.                  HANDLE          hFind;
  164.                  SYSTEMTIME      sysTime;
  165.                  FILETIME        fLocTime;
  166.                  WIN32_FIND_DATA fd;
  167.                  PAINTSTRUCT     ps;
  168.                  int             yPos = 0;
  169.                  BOOL            bRet = TRUE;
  170.  
  171.                  BeginPaint( hWnd, &ps );
  172.  
  173.                  // Start listing of files.
  174.                  //........................
  175.                  hFind = FindFirstFile( "D:\\TEMP\\*.*", &fd );
  176.  
  177.                  // While there are more files, continue processing.
  178.                  //.................................................
  179.                  while ( hFind != INVALID_HANDLE_VALUE && bRet )
  180.                  {
  181.                     // Display the file and its information if this is
  182.                     // not a directory that we found.
  183.                     //................................................
  184.                     if ( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
  185.                     {
  186.                        // Convert the file time to a local time, then to
  187.                        // a SYSTEMTIME structure so we can display it.
  188.                        //...............................................
  189.                        FileTimeToLocalFileTime( &fd.ftLastWriteTime, 
  190.                                                 &fLocTime );
  191.                        FileTimeToSystemTime( &fLocTime, &sysTime );
  192.  
  193.                        // Output the fileÆs information.
  194.                        //...............................
  195.                        yPos += 15;
  196.                        wsprintf( szOut, 
  197.                                  "%s \t %.2d/%.2d/%.4d \t %.2d:%.2d:%.2d \t %d", 
  198.                                  fd.cFileName, 
  199.                                  sysTime.wMonth,
  200.                                  sysTime.wDay,
  201.                                  sysTime.wYear,
  202.                                  sysTime.wHour,
  203.                                  sysTime.wMinute,
  204.                                  sysTime.wSecond,
  205.                                  fd.nFileSizeLow );
  206.  
  207.                        TabbedTextOut( ps.hdc, 10, yPos, szOut, 
  208.                                       strlen( szOut ), 1, &nTab, 10 );
  209.                     }
  210.  
  211.                     // Find the next file.
  212.                     //....................
  213.                     bRet = FindNextFile( hFind, &fd );
  214.                  }
  215.  
  216.                  // Close the find.
  217.                  //................
  218.                  FindClose( hFind );
  219.  
  220.                  EndPaint( hWnd, &ps );
  221.               }
  222.               break;
  223.  
  224.       case WM_COMMAND :
  225.               switch( LOWORD( wParam ) )
  226.               {
  227.                  case IDM_ABOUT :
  228.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  229.                         break;
  230.  
  231.                  case IDM_EXIT :
  232.                         DestroyWindow( hWnd );
  233.                         break;
  234.               }
  235.               break;
  236.       
  237.       case WM_DESTROY :
  238.               // Terminate thread that is waiting for changes.
  239.               //..............................................
  240.               if ( hThread )
  241.                  TerminateThread( hThread, 0 );
  242.  
  243.               // Close the change notification.
  244.               //...............................
  245.               if ( hWait )
  246.                  FindCloseChangeNotification( hWait );
  247.  
  248.               PostQuitMessage(0);
  249.               break;
  250.  
  251.       default :
  252.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  253.    }
  254.  
  255.    return( 0L );
  256. }
  257.  
  258.  
  259. LRESULT CALLBACK About( HWND hDlg,           
  260.                         UINT message,        
  261.                         WPARAM wParam,       
  262.                         LPARAM lParam)
  263. {
  264.    switch (message) 
  265.    {
  266.        case WM_INITDIALOG: 
  267.                return (TRUE);
  268.  
  269.        case WM_COMMAND:                              
  270.                if (   LOWORD(wParam) == IDOK         
  271.                    || LOWORD(wParam) == IDCANCEL)    
  272.                {
  273.                        EndDialog(hDlg, TRUE);        
  274.                        return (TRUE);
  275.                }
  276.                break;
  277.    }
  278.  
  279.    return (FALSE); 
  280. }
  281.